package com.struts.deal;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;

import com.struts.web.user.register.UserPictureForm;
import com.lg.system.*;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.po.user.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.*;

public class UserDeal {
	
	/**
	 * ����ͼƬ
	 * @param request
	 * @param aWebForm
	 */
    public static void doUserPicture(HttpServletRequest request,UserPictureForm aWebForm){
    	String imageURL  = "";
    	String imageId = "";
    	SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
    	String pictureStatus = "0";
    	String dir = request.getRealPath("/userpicture");
    	
        /** �ϴ����ļ� */
        FormFile file = aWebForm.getPicture();
        /** ������������ڶ�ȡ�ϴ����ļ���ݣ� */
        InputStream in = null;
        /** ��������(���ڽ���ȡ�����ļ����������˵���һ���ļ���) */
        OutputStream out = null;
        
    	try{
    		TUserInfo user = (TUserInfo)request.getSession().getAttribute("TUser");
        	if(user!=null){
        	
       
    	//图片的类型
    	String imageType=aWebForm.getPicture().getContentType();
    	if(imageType!=null && imageType.length()>0){
    		if(imageType.equals("image/pjpeg")){
    			imageType = ".jpg";
    		}else{
    			imageType = ".gif";
    		}			
    	}
    		//处理图片
    	TUserInfo user1 = (TUserInfo)sqlmap.queryForObject("T_USER_INFO.selectImage",user);
    	//存在图片
    	if(user1.getImageURL()!=null){
    		imageURL = user1.getImageURL();
    	}else{//不存在图片
    		    imageId = Common.getNewID();
	    		imageURL = imageId+imageType;
	    		user.setImageURL(imageURL);
		        sqlmap.update("T_USER_INFO.updateImageURL",user);
    	}
		    in = file.getInputStream();
	    	Image src = javax.imageio.ImageIO.read(in);
	    	BufferedImage tag = new BufferedImage(80,90,BufferedImage.TYPE_INT_RGB);
	    	tag.getGraphics().drawImage(src,0,0,80,90,null);       
	    	out = new FileOutputStream(dir+"/"+imageURL);
	    	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
	    	encoder.encode(tag);
	    	out.close();
    	}
    	
    	loadPicture(request,aWebForm);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    
    /**
     * ����ͼƬ��Ϣ
     */
    public static void loadPicture(HttpServletRequest request,UserPictureForm aWebForm) throws SQLException {
    	TUserInfo user = (TUserInfo)request.getSession().getAttribute("TUser");
    	SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
    	try{
    	if(user!=null){
    	     user=(TUserInfo)sqlmap.queryForObject("T_USER_INFO.abatorgenerated_selectByPrimaryKey",user);
    	     if(user.getImageURL()!=null){
    	    	 aWebForm.setPictureUrl("/userpicture/"+user.getImageURL());
    	     }else{
	    	aWebForm.setPictureUrl("/userpicture/nopicture.gif");
    	     }
    	}else{
    		aWebForm.setPictureUrl("/userpicture/nopicture.gif");
    	}
    	}catch(Exception e){
    		e.printStackTrace();
    		}
    	
    }
}
